//
//  VisitorLoginView.swift
//  weibo
//
//  Created by ForeverOSS on 16/2/12.
//  Copyright © 2016年 foreveross. All rights reserved.
//

import UIKit
///访客登录视图代理
protocol VisitorLoginViewDelegate: NSObjectProtocol {
    /// 访客视图注册
    func visitorLoginViewDidRegister();
    /// 访客视图登录
    func visitorLoginViewDidLogin();
}
///访客视图
class VisitorLoginView: UIView {
    
    //代理
    weak var delegate:VisitorLoginViewDelegate?
///MARK: - 设置界面
    override init(frame:CGRect){
    
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    // MARK: - 设置视图信息
    /// 设置访客视图信息
    ///
    /// - parameter imageName: 图像名称，如果是首页传入 nil
    /// - parameter message:   描述文字
    func setupInfo(imageName: String?,message: String){
    
        messageLabel.text = message
        
        if let imageName = imageName{
        
            iconView.image = UIImage(named: imageName)
            homeIconView.hidden = true
            sendSubviewToBack(maskIconView)
        }else{
        
            startAnimation()
        }
    }
    
    ///开始动画
    private func startAnimation(){
    
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 20
        animation.removedOnCompletion = false
        iconView.layer.addAnimation(animation, forKey: nil)
    }
    
    /// 设置界面
    
    private func setupUI(){
    
        // 1、添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //添加按钮监听方法
        registerButton.addTarget(self, action: "register", forControlEvents: .TouchUpInside)
        
        loginButton.addTarget(self, action: "login", forControlEvents: .TouchUpInside)
        
        
        // 2. 自动布局
        // 0> 取消 aotoresizing 属性
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        // 1> 图标
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
    
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        
        // 2> 房子
        
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        
        // 3> 描述文字
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        
        //4> 注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Left, relatedBy: .Equal, toItem: messageLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        
        //5> 登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Right, relatedBy: .Equal, toItem: messageLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        
        //6> 遮罩视图
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[maskIconView]-0-|", options: [], metrics: nil, views: ["maskIconView":maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[maskIconView]-(regBtnHeight)-[registerButton]", options: [], metrics: ["regBtnHeight":-36], views: ["maskIconView":maskIconView,"registerButton":registerButton]))
        
        // 3.设置视图背景颜色
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
    }
    
    //MARK: - 懒加载控件
    ///图标图片
    private lazy var iconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    ///小房子
    private lazy var homeIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    ///描述文字
    private lazy var messageLabel: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", fontSize: 14, color: UIColor.darkGrayColor())
    ///注册按钮
    
    private lazy var registerButton: UIButton = UIButton(title: "注册", color: UIColor.orangeColor(), fontSize: 14, backImageName: "common_button_white_disable")
    
    
    ///登录按钮
    
    private lazy var loginButton: UIButton = UIButton(title: "登录", color: UIColor.orangeColor(), fontSize: 14, backImageName: "common_button_white_disable")
    
    ///遮罩视图
    private lazy var maskIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    
    //MARK: - 监听方法
    @objc private func register(){
    
        delegate?.visitorLoginViewDidRegister()
    }
    @objc private func login(){
    
        delegate?.visitorLoginViewDidLogin()
    }
    
    
}



